import pymongo
from prettytable import PrettyTable

print('''
============学生信息管理系统============\n
------------功能菜单---------------\n
1.录入学生信息
2.查找学生信息
3.删除学生信息
4.修改学生信息
5.排序
6.统计学生总人数
7.显示所有学生信息
0.退出系统
----------------------------
''')


# 创建客户端
client = pymongo.MongoClient('mongodb://127.0.0.1:27017')

# 指定数据库
studentDB = client.studentDB

# 指定集合（表）
students_collection  = studentDB.students


def Information():

    # print(f'现在输入第{index}个学生的信息')
    student_information_obj = {} # 接收学生信息的字典
    student_id = int(input('请输入学生ID:'))
    name = input('请输入姓名:')
    python_achievement = int(input('请输入python成绩:'))
    student_information_obj['student_id'] = student_id
    student_information_obj['name'] = name
    student_information_obj['python_achievement'] = python_achievement

    student_result = students_collection.find()
    
    for result in student_result:
      if result['student_id'] == student_information_obj['student_id']:
        print('该学生ID与数据库中的ID有重复,请重新输入')
        return True

    students_collection.insert_one(student_information_obj)
    print(f'学生的信息已录入完成')
    
    choose = input('是否继续添加学生?(y/n):')
    if choose == 'y':
      return True
    elif choose == 'n':
      return False
    else:
      print('输入的字符不符合要求,停止添加')
      return False
    
def find_student_information():
  num = int(input('按ID查询请按1,按姓名查询请按2:'))
  if num == 1:
    student_id = int(input('请输入学生ID:'))
    result = students_collection.find_one({'student_id': student_id})
    if result == None:
      print('没有查询到相应的学生')
    else:
      table = PrettyTable(['学生ID', '姓名', 'python成绩'])
      table.add_row([result['student_id'], result['name'], result['python_achievement']])
      print(table)
  else:
    student_name = input('请输入学生姓名:')
    result = students_collection.find_one({'name': student_name})
    if result == None:
      print('没有查询到相应的学生')
    else:
      table = PrettyTable(['学生ID', '姓名', 'python成绩'])
      table.add_row([result['student_id'], result['name'], result['python_achievement']])
      print(table)

  choose = input('是否继续查询学生?(y/n):')
  if choose == 'y':
    return True
  elif choose == 'n':
    return False
  else:
    print('输入的字符不符合要求,停止查询')
    return False

def delete_student():
  student_id = int(input('请输入需要删除的学生ID:'))
  students_collection.delete_one({'student_id': student_id})
  print('学生已被删除')
  choose = input('是否继续删除学生?(y/n):')
  if choose == 'y':
    return True
  elif choose == 'n':
    return False
  else:
    print('输入的字符不符合要求,停止删除')
    return False

def show_students():
  table = PrettyTable(['学生ID', '姓名', 'python成绩'])
  result = students_collection.find()
  for x in result:
    table.add_row([x['student_id'], x['name'], x['python_achievement']])
  print(table)

def sorts():
  table = PrettyTable(['学生ID', '姓名', 'python成绩'])
  result = students_collection.find()
  choose = int(input('请选择(0升序,1降序):'))
  if choose == 0:
    array = sorted(result,key=lambda x:x['python_achievement'],reverse=False)
    for x in array:
      table.add_row([x['student_id'], x['name'], x['python_achievement']])
    print(table)
    return False
  elif choose == 1:
    array = sorted(result,key=lambda x:x['python_achievement'],reverse=True)
    for x in array:
      table.add_row([x['student_id'], x['name'], x['python_achievement']])
    print(table)
    return False
  else:
    print('数字输入错误,请重新输入')
    return True

def update_student():
  student_id = int(input('请输入要修改的学生ID:'))
  result = students_collection.find_one({'student_id': student_id})
  if result == None:
    print('没有查询到相应的学生,请重新查找')
    return True
  else:
    print('已查找到这名学生,可以修改他的相关信息')
    name = input('请输入姓名:')
    python_achievement = int(input('请输入python成绩:'))
    students_collection.update_one({'student_id':result['student_id']},{ '$set': { 'name': name,'python_achievement': python_achievement }})
    print('修改完成！')
    choose = input('是否继续修改学生?(y/n):')
    if choose == 'y':
      return True
    elif choose == 'n':
      return False
    else:
      print('输入的字符不符合要求,停止更新')
      return False
    

while True:

    num = int(input('请选择(0-7):'))

    if num == 1:
      flag = True
      while flag:
        flag = Information()
    
    elif num == 2:
      student_result = students_collection.estimated_document_count()
      if student_result == 0:
        print('没有学生信息,请去录入学生信息')
      else:
        flag = True
        while flag:
          flag = find_student_information()
        
        
    elif num == 3:
      student_result = students_collection.estimated_document_count()
      if student_result == 0:
        print('没有学生信息,请去录入学生信息')
      else:
        flag = True
        while flag:
          flag = delete_student()

    elif num == 4:
      student_result = students_collection.estimated_document_count()
      if student_result == 0:
        print('没有学生信息,请去录入学生信息')
      else:
        flag = True
        while flag:
          flag = update_student()
    
    elif num == 5:
      student_result = students_collection.estimated_document_count()
      if student_result == 0:
        print('没有学生信息,请去录入教师信息')
      else:
        flag = True
        while flag:
          flag = sorts()

    elif num == 6:
      student_result = students_collection.estimated_document_count()
      if student_result == 0:
        print('没有学生信息,请去录入教师信息')
      else:
        print(f'学生总人数为{student_result}')

    elif num == 7:
      student_result = students_collection.estimated_document_count()
      if student_result == 0:
        print('没有学生信息,请去录入教师信息')
      else:
        show_students()

    elif num == 0:
        print('系统已退出')
        break

    else:
        print('输入的数字系统中没有，程序结束')
        break